package com.zhoug.common3.utils;

import android.graphics.Bitmap;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * io流工具
 */
public class IOUtils {

    /**
     * 保存文件
     *
     * @param resultPath 保存的路径
     * @param bytes      文件字节数组
     * @param append     是否在源文件末尾开始保存
     * @return
     */
    public static boolean keepFile(String resultPath, byte[] bytes, boolean append) {
        FileOutputStream fos = null;
        boolean success = false;
        File parentFile = new File(resultPath).getParentFile();
        if (!parentFile.exists()) {
            boolean mkdirs = parentFile.mkdirs();
            parentFile = null;
        }
        try {
            fos = new FileOutputStream(resultPath, append);
            fos.write(bytes, 0, bytes.length);
            fos.flush();
            success = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return success;
    }

    /**
     * 保存文件
     *
     * @param resultPath 保存的路径
     * @param inputStream      文件字节数组
     * @param append     是否在源文件末尾开始保存
     * @return
     */
    public static boolean keepFile(String resultPath, InputStream inputStream, boolean append) {
        FileOutputStream fos = null;
        boolean success = false;
        File parentFile = new File(resultPath).getParentFile();
        if (!parentFile.exists()) {
            boolean mkdirs = parentFile.mkdirs();
            parentFile = null;
        }
        try {
            fos = new FileOutputStream(resultPath, append);
            byte[] bytes=new byte[1024];
            int len=0;
            while ((len=inputStream.read(bytes))!=-1){
                fos.write(bytes, 0,len);
            }
            fos.flush();
            success = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(inputStream);
            close(fos);
        }
        return success;
    }



    /**
     * 保存文件
     *
     * @param resultPath 保存的路径
     * @param bytes      文件字节数组
     * @return
     */
    public static boolean keepFile(String resultPath, byte[] bytes) {
        return keepFile(resultPath, bytes, false);
    }

    /**
     * 复制文件
     *
     * @param sourcePath 资源文件路径
     * @param resultPath 最终文件路径
     * @return
     */
    public static boolean copyFile(String sourcePath, String resultPath) {
        boolean success = false;
        //首先创建文件夹
        File parentFile = new File(resultPath).getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        FileOutputStream fos = null;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(sourcePath);
            fos = new FileOutputStream(resultPath);
            byte[] buf = new byte[1024 * 10];
            int len = 0;
            while ((len = fis.read(buf)) != -1) {
                fos.write(buf, 0, len);
            }
            fos.flush();
            success = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null)
                    fos.close();
                if (fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return success;
    }



    /**
     * InputStream 转为byte数组
     *
     * @param ins
     * @return
     */
    public static byte[] inputStreamToByteArray(InputStream ins) {
        if (ins != null) {
            ByteArrayOutputStream output = null;
            try {
                output = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = ins.read(buffer)) != -1) {
                    output.write(buffer, 0, len);
                }
                return output.toByteArray();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                close(ins);
                close(output);
            }
        }
        return null;
    }

    /**
     * copy
     * @param in 输入流
     * @param out 输出流
     */
    public static void copy(InputStream in, OutputStream out) throws Exception {
       copy(in,out,true);
    }

    /**
     * copy
     * @param in 输入流
     * @param out 输出流
     * @param autoClose 自动关闭输入输出流
     */
    public static void copy(InputStream in, OutputStream out,boolean autoClose) throws Exception {
        try {
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.flush();
        }finally {
            if(autoClose){
                close(out);
                close(in);
            }

        }
    }

    /**
     *
     * Bitmap 保存为文件
     *
     * @param bitmap
     * @param path   bitmap保存路径
     * @return
     */
    private boolean bitmapToPath(Bitmap bitmap, String path) {
        FileOutputStream fos = null;
        boolean compress;
        try {
            fos = new FileOutputStream(path);
            compress = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            if (compress) {
                fos.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
            compress = false;
        } finally {
            IOUtils.close(fos);
        }
        return compress;
    }

    public static void close(Closeable closeable) {
        try {
            if (closeable != null) {
                closeable.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
